package top.someapp.arch;

/**
 * @author zwz
 * Created on 2021-07-22
 */
public final class Optional<T> {

    public static final Optional<?> EMPTY = new Optional<>(null);
    private final T value;

    private Optional(T value) {this.value = value;}

    @SuppressWarnings("unchecked")
    public static <T> Optional<T> of(T value) {
        return value == null ? (Optional<T>) EMPTY : new Optional<>(value);
    }

    public static <T> T notNull(T value) {
        if (value == null) throw new NullPointerException();
        return value;
    }

    public T notNull() {
        if (value == null) throw new AppException("Require @NonNull, but value absent!");
        return value;
    }

    public T ifNull(T valueIfNull) {
        return value == null ? valueIfNull : value;
    }

    public T nullable() {
        return value;
    }
}
